﻿using System;
using System.Collections;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;
using System.Collections.Generic;
using CoyoEden.Web.Controls;
using CoyoEden.Web.Controller;
using CoyoEden.Web.Data;
using CoyoEden.Models;
using Vivasky.Core.Infrastructure;

namespace CoyoEden.Web.Services
{
	/// <summary>
	/// Summary description for Category
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	[ScriptService]
	public class Community : WebService
	{

		#region Flexigrid
		/// <summary>
		/// Get Categores,and return FlexigridData for jquery's flexigrid plugin
		/// </summary>
		/// <param name="Params"></param>
		/// <returns></returns>
		/// <remarks>
		/// Client Js invoking sample:
		/// $.ajax({
		///		type:'post',
		///		url:xxx/yyy.asmx/GetCategories,
		///		contentType:'application/json;charset=utf-8',
		///		dataType:'json',
		///		data:'{"Params":{"x":"1","y":"2"}}',
		///		success:callback1,
		///		error:callback2
		/// });
		/// </remarks>
		[WebMethod]
		public FlexigridData<Models.Community> GetCommunities(FlexiGridInputData Params)
		{
			var c = Models.Community.Communities.OrderByDescending(item => item.DateCreated).AsQueryable();
			var data = new FlexigridData<Models.Community>(c, Params);

			return data;
		}

		#endregion

		#region Methods
		[WebMethod]
		public FeedbackData PreEdit(Dictionary<string, string> Params)
		{

			FeedbackData retVal = new FeedbackData();
			Models.Community tempObj = CommunityController.Single(Params, out retVal);
			if (tempObj != null)
			{
				ViewManager<CommunityViewBase> vm = new ViewManager<CommunityViewBase>("~/Views/CommunityDetail.ascx");
				vm.Control.TheCommunity = tempObj;
				retVal.Body = vm.Render();
			}
			return retVal;
		}
		[WebMethod]
		public FeedbackData Update(List<NameValuePair<string, string>> Params)
		{
			FeedbackData retVal = new FeedbackData();
			CommunityController.Update(Params, out retVal);
			return retVal;
		}
		[WebMethod]
		public FeedbackData Delete(Dictionary<string, string> Params)
		{
			FeedbackData retVal = new FeedbackData();
			CommunityController.Delete(Params, out retVal);
			return retVal;
		}
		#endregion

	}
}